﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using Microsoft.SharePoint;
using System.Collections.Generic;
using System.Linq;

namespace WebpartSample.PhotosWebpart
{
    public partial class PhotosWebpartUserControl : UserControl
    {
        public int MaxVal;
        string FirstImg, FirstTxt;

        protected void Page_Load(object sender, EventArgs e)
        {
            SPWeb oWeb = SPContext.Current.Web;
            SPList oList = oWeb.Lists.TryGetList("PicLib");
            if (oList != null)
            {
                SPListItemCollection spli=oList.GetItems();
                System.Text.StringBuilder imgs = new System.Text.StringBuilder();
                System.Text.StringBuilder Txt = new System.Text.StringBuilder();
                System.Text.StringBuilder sb = new System.Text.StringBuilder();
                string tst=string.Empty;
                foreach (SPListItem item in spli)
                {
                    if (Txt.ToString() != string.Empty)
                    {
                        Txt.Append(",");
                        imgs.Append(",");
                    }
                    else
                    {
                        FirstImg = item.Url;
                        try
                        {
                            FirstTxt = item["Name"].ToString();
                        }
                        catch (Exception) { FirstTxt = ""; }
                    }
                    imgs.Append(string.Format("'/{0}'", item.Url));
                    
                    try
                    {
                        tst = item["Name"].ToString();
                    }
                    catch (Exception) { tst = ""; }
                    Txt.Append(string.Format("'{0}'", tst));
                }
                MaxVal = spli.Count;
                sb.Append(string.Format("<script> imgs=[{0}];txt=[{1}];max={2};",imgs,Txt,MaxVal));
                Literal2.Text = string.Format("<img src='/{0}' alt='' id='imgDisp' onclick='callmePopup()'  class='WidGetGp' />" +
                                                "<div style='position:relative;top:-30px;text-align:center;' id='spanTex' >{1}</div>", FirstImg, FirstTxt);
                Literal1.Text = sb.ToString();
            }
        }
    }
}
